package com.yurendao.shop.user.web;

import com.alibaba.fastjson.JSONObject;
import com.yurendao.shop.user.domain.User;
import com.yurendao.shop.user.service.UserService;
import com.yurendao.shop.user.service.exception.UserException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

/**
 * Created by kong on 15/10/5.
 */
@RequestMapping("/user")
@Controller
public class UserController {

    @Autowired
    private UserService userService;


    @RequestMapping(value = "/login",method = RequestMethod.GET)
    public String login() throws IOException {
        return "user/login";
    }
    /**
     * 登录
     * @param user
     * @return
     */
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String login(User user,ModelMap map,HttpServletRequest req){
        try {
            System.out.println(user.getUname()+"  "+user.getPassword());
            User loginuser = userService.login(user);
            req.getSession().setAttribute("loginuser",loginuser);
        } catch (UserException e) {
            map.addAttribute("error",e.getMessage());
            System.out.println(e.getMessage());
            return "/user/login";
        }
        return "/luntan/luntan";
    }

    @RequestMapping(value = "/regist",method = RequestMethod.GET)
    public String regist() throws IOException {
            return "user/register";
    }

    /**
     * 注册
     * @param user
     * @param map
     * @return
     */
    @RequestMapping(value = "/regist",method = RequestMethod.POST)
    public String regist(User user,ModelMap map){
        try {
            userService.insertUser(user);
        } catch (UserException e) {
            map.addAttribute("error",e);
            return "/user/regist";
        }
        return "/main";
    }

    /**
     * 手机发送验证码
     * @param telephone
     */
    @RequestMapping(value = "/telephonevcode",method = RequestMethod.POST)
    public void ajaxTelephoneVcode(String telephone,HttpServletRequest req){
        try {
            userService.telephoneRegist(telephone);
        } catch (UserException e) {
            e.printStackTrace();
        }
    }

    /**
     * AJAX验证用户名
     * @param uname
     */
    @RequestMapping(value = "/ajaxUname",method = RequestMethod.POST)
    public void ajaxUname(String uname){

    }

    /**
     * AJAX验证验证码
     * @param vcode
     */
    @RequestMapping(value = "ajaxVcode",method = RequestMethod.POST)
    public void ajaxVcode(String vcode){
    }

    @RequestMapping(value = "/getuser")
    @ResponseBody
    public User getUser(){

        System.out.println("kongyaolang");
        User user = new User();
        user.setPassword("123");
        user.setUname("kongyaolang");
        user.setActivitycode("safdsabvbsdauibvsdabvasdv");
        user.setUstauts(true);
        return user;
    }

    /**
     * 退出登录
     * @param request
     * @param response
     */
    @RequestMapping(value="/exit",method=RequestMethod.GET)
    public String logout(HttpServletRequest request,HttpServletResponse response)
    {
        Enumeration<String> em = request.getSession().getAttributeNames();
        while(em.hasMoreElements()){
            request.getSession().removeAttribute(em.nextElement().toString());
        }
        request.getSession().invalidate();
        String path = request.getContextPath();
        String basePath = request.getScheme() + "://"
                + request.getServerName() + ":" + request.getServerPort()
                + path + "/";
        String str = "<script>top.location='"+basePath+"'</script>";
        System.out.println(str);
        responseTxt(response,str);
        return "/main";
    }

    protected void responseTxt(HttpServletResponse response, String str) {
        try {
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter out = response.getWriter();
            out.write(str);
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RequestMapping(value = "/getName",method = RequestMethod.GET)
    @ResponseBody
    public JSONObject getUnameByUid(String uid){
        JSONObject jsonObject = new JSONObject();
        try {
            String uname = userService.getNameByID(uid);
            jsonObject.put("uname",uname);
            jsonObject.put("error_code",0);
        } catch (UserException e) {
            jsonObject.put("error_code",e.getMessage());
        }
        return jsonObject;
    }
}
